package com.example.jiaju.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.jiaju.R;
import com.example.jiaju.entity.Furniture;
import com.example.jiaju.ui.main.ConcreteFurnitureActivity;

import java.util.ArrayList;
import java.util.List;

public class FurnitureRecommendAdapter extends RecyclerView.Adapter<FurnitureRecommendAdapter.MyViewHolder>{

    private List<Furniture.PageDTO.ListDTO> list=new ArrayList<>();
    private Context context;

    public FurnitureRecommendAdapter(List<Furniture.PageDTO.ListDTO> list, Context context) {
        this.list = list;
        this.context=context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.furniture_recommend,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        //填充数据
        Furniture.PageDTO.ListDTO furnDTO=list.get(position);
        holder.furnBrand.setText(furnDTO.furnBrand);
        holder.furnName.setText(furnDTO.furnName);
        holder.furnPrice.setText("￥"+furnDTO.furnPrice.toString());
        Glide.with(holder.furnImg).load(furnDTO.furnImg).into(holder.furnImg);
        holder.furnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ConcreteFurnitureActivity.class);
                intent.putExtra("furnDTO",furnDTO);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView furnImg;
        private TextView furnBrand;
        private TextView furnName;
        private TextView furnPrice;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            furnImg=itemView.findViewById(R.id.furn_img);
            furnBrand=itemView.findViewById(R.id.furn_brand);
            furnName=itemView.findViewById(R.id.furn_name);
            furnPrice=itemView.findViewById(R.id.furn_price);
        }
    }
}
